package entity;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * An alert region is a hash set of cell id's
 * 
 * @author xiaohui
 * 
 */
public class AlertRegion {

	List<Cell> region;

	public AlertRegion() {
		region = new ArrayList<Cell>(32);
	}

	public AlertRegion(ArrayList<Cell> region) {
		this.region = region;
	}

	public void add(Cell cell) {
		boolean dup = false;
		for (Cell e : region) {
			if (e.equals(cell)) {
				dup = true;
			}
		}
		if (!dup) {
			region.add(cell);
		}
	}

	public boolean contains(Cell cell) {
		boolean dup = false;
		for (Cell e : region) {
			if (e.equals(cell)) {
				dup = true;
			}
		}
		return dup;
	}

	public List<Cell> getRegion() {
		return region;
	}

	public int indexOf(Cell cell) {
		int i = 0;
		int index = 0;
		for (Cell e : region) {
			if (e.equals(cell)) {
				index = i;
				break;
			}
			i++;
		}
		return index;
	}

	public void remove(Cell cell) {
		region.remove(indexOf(cell));
	}

	public String toString() {
		String res = "[";
		for (Cell e : region) {
			res += e.toString() + " ";
		}
		return res + "]";
	}

	public void setRegion(ArrayList<Cell> newRegion) {
		this.region = newRegion;
	}

	public static void main(String[] args) {
		Cell c1 = new Cell(1, 2, 0);
		Cell c2 = new Cell(3, 4, 0);
		Cell c3 = new Cell(1, 2, 0);
		AlertRegion ar = new AlertRegion();
		ar.add(c1);
		ar.add(c2);
		ar.add(c3);
		System.out.println(ar);
		System.out.println(ar.indexOf(c2));
		ar.remove(new Cell(1, 2, 3));
		System.out.println(ar);

	}

}
